<template>
    <div>
        <el-select v-if="type==1" v-model="sval"  :placeholder="placeholder" @change="changeData" :style="{width}" :disabled="disabled">
            <el-option v-for="dt in tableData" :key="dt.arLandmarkId" :label="dt.name" :value="dt.arLandmarkId" @click.native="item=dt"></el-option>
        </el-select>
        <div v-else @click="openDg">
            <el-input v-model="landmarkNames" placeholder="请选择AR地标" maxlength="50" type="text" class="headInput" :readonly="true"  :style="{width}" :disabled="disabled"/>
        </div>
     
     
        <my-teleport to="body" v-if="lkFlag">
            <my-dialog :show.sync="lkFlag" :showBtn="type==2" width="500px" title="请选择AR地标" @confirm="getSelectData">   
                <my-table ref="tableEle" :loading="loading" :lists="tableData" @selectionChange="tableSelect" @pageChange="handleChange" :showCheck="type==2" :sequence="true" :page='page' selectKey="arLandmarkId|name">
                    <el-table-column property="arLandmarkId" label="地标ID" align='center' width="70px"></el-table-column>
                    <el-table-column property="name" label="地标名称" align='left'></el-table-column>
                    <el-table-column v-if="type==3" prop="status" label="操作" align="center" width="100px">
                        <template slot-scope="scope">
                            <el-button size="mini" type="success" plain @click="dgSelectData(scope.row)">选择</el-button>
                        </template>
                    </el-table-column>
                </my-table>
            </my-dialog> 
        </my-teleport>
    </div>
</template>

<script>
import {mapGetters} from 'vuex'
import {
  getLandmarkList
} from '@/api/arApi'
import myDialog from '@/components/public/myDialog';
import myTable from '@/components/public/myTable';
import myTeleport from './myTeleport.vue';
export default {
    props:{
        value:'',
        type:{  // 选择类型：1为select选择，2为弹窗多选、3为弹窗单选
            type:Number,
            default:1   
        },
        placeholder:{
            type:String,
            default:'请选择'
        },
        width:{
            type:String,
            default:'100%'
        },
        defaultTxt:String,
        disabled:{
            type:Boolean,
            default:false
        }
    },
    components:{
        myDialog,
        myTable,
        myTeleport
    },
    data(){
        return{
            landmarkNames:'',
            lkFlag:false,
            page:{
                pageSize:10,
                pageIndex:1,
			    total:0,
            },
            loading:true,
            tableData:[],
            multipleSelectionIds:[],
            sval:'',
        }
    },
    computed:{
        ...mapGetters({
            'goodsAddParams':'goods/goodsAddParams'
        })
    },
    created(){
        this.sval = this.value
        this.defaultTxt && (this.landmarkNames = this.defaultTxt)
        this.getList()
    },
    watch:{
        defaultTxt(n,o){
            this.landmarkNames = n
        }
    },
    methods:{
        async getList(){
            const params = {
                limit: this.type==1?1000:this.page.pageSize,
                page: this.page.pageIndex
            }
            const res = await getLandmarkList(params)  // 获取地标
            this.loading = false
            this.tableData = res.data
            this.page.total = res.total
            
            
        },
        openDg(){
            if(this.disabled) return
            this.lkFlag=true
            // 设置表格默认选项
            this.$nextTick(()=>{
                if(this.value && this.tableData && this.tableData.length>0){
                    if(this.type ==2){ // 表格多选
                        this.tableData.forEach(row => {
                            if(this.value.indexOf(row.arLandmarkId)>-1){
                                this.$refs.tableEle.setRowSelection(row);
                            }
                        });
                    }else if(this.type ==3){ // 表格单选
                        this.tableData.forEach(row => {
                            if(this.value == row.arLandmarkId){
                                this.$refs.tableEle.setRowSelection(row);
                                //this.landmarkNames = row.name
                            }
                        });
                    }
                }
            })
        },
        getSelectData(){
            this.lkFlag = false
        },
        tableSelect(obj,items){
           // console.log("obj:",obj)
           // console.log("items:",items)
           this.landmarkNames = obj.name.join(',')
           this.$emit('input',obj.arLandmarkId)
        },
        handleChange(index) {
            this.page.pageIndex = index;
            this.getList();
        },
        changeData(val){
            this.$emit('input',val)
        },
        dgSelectData(item){
            this.landmarkNames = item.name
            this.$emit('input',item.arLandmarkId)
            this.lkFlag = false
        },
        clear(){
            this.sval=''
            this.landmarkNames = ''
            this.$emit('input','')
        }
    }
}
</script>

<style lang="scss" scoped>
    .public_head {
            ul {
                padding: 0;
                display: flex;
                flex-wrap: wrap;
                padding: 30px 50px 10px 84px;
                box-sizing: border-box;
                margin: 0;
                li {
                    display: flex;
                    width: 50%;
                    margin-bottom: 20px;
                    align-items: center;
                    p {
                        width: 120px;
                        span {
                            color: #ff0000;
                        }
                    }
                    .headInput {
                        width: 400px;
                    }
                }
            }
        }
</style>